// Frame.cc

/*
   Copyright 2006 Corey Tabaka

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include <Frame.h>
#include <Graphics.h>
#include <string.h>

namespace gui {

// define some window geometry constants for now. later this can be configurable
#define FRAME_INSET 5
#define FRAME_TITLEBAR_HEIGHT 23
#define FRAME_BLUR_RADIUS 4
#define FRAME_EDGE_RADIUS 5
#define FRAME_BORDER 1
#define FRAME_TITLE_OFFSET_X 19
#define FRAME_TITLE_OFFSET_Y 15
#define FRAME_CONTENT_BORDER 1

static Color FRAME_BACKGROUND_COLOR(0xC5, 0xDD, 0xF4, 0x80);
static Color FRAME_BORDER_COLOR(0x06, 0x10, 0x1B, 0x80);
static Color FRAME_TEXT_HIGHLIGHT(0x06, 0x10, 0x1B, 0x80);
static Color FRAME_TEXT_COLOR(0xff, 0xff, 0xff);

RefFrame Frame::head;
RefFrame Frame::tail;

Frame::Frame(const char *title) {
	this->title = new char[strlen(title)+1];
	strcpy(this->title, title);
	
	//content.setLocation(5, 16);
	content.setLocation(FRAME_INSET + FRAME_CONTENT_BORDER,
		FRAME_TITLEBAR_HEIGHT + FRAME_CONTENT_BORDER);
	
	setBackground(Color(0xff, 0xff, 0xff));
}

Frame::~Frame(void) {
	delete title;
}

RefContainer Frame::getContentPane(void) {
	return content.getRef();
}

void Frame::validate(void) {
	if (!valid) {
		content.validate();
	}
}

void Frame::invalidate(void) {
	content.invalidate();
}

void Frame::repaint(RefGraphics g) {
	int width = getWidth();
	int height = getHeight();
	
	g->setOffset(rect.x1, rect.y1);
	
	g->blurRect(FRAME_BORDER, FRAME_BORDER, width - FRAME_BORDER*2,
		height - FRAME_BORDER*2, FRAME_BLUR_RADIUS);
	
	g->setColor(FRAME_BACKGROUND_COLOR);
	g->fillRect(FRAME_BORDER, FRAME_BORDER, width - FRAME_BORDER*2,
		height - FRAME_BORDER*2, FRAME_EDGE_RADIUS);
	g->setColor(FRAME_BORDER_COLOR);
	g->drawRect(0, 0, width, height, FRAME_EDGE_RADIUS);
	
	paint(g);
}

void Frame::paint(RefGraphics g) {
	int width = getWidth();
	int height = getHeight();
	
	g->setColor(FRAME_TEXT_HIGHLIGHT);
	g->drawString(title, FRAME_TITLE_OFFSET_X + 1, FRAME_TITLE_OFFSET_Y + 1);
	g->setColor(FRAME_TEXT_COLOR);
	g->drawString(title, FRAME_TITLE_OFFSET_X, FRAME_TITLE_OFFSET_Y);
	
	RefGraphics g2 = g->create(FRAME_INSET + FRAME_CONTENT_BORDER,
		FRAME_TITLEBAR_HEIGHT + FRAME_CONTENT_BORDER,
		width - (FRAME_INSET*2 + FRAME_CONTENT_BORDER*2),
		height - (FRAME_INSET + FRAME_CONTENT_BORDER*2 + FRAME_TITLEBAR_HEIGHT));
	content.repaint(g2);
	
	g->setColor(FRAME_BORDER_COLOR);
	g->drawRect(FRAME_INSET, FRAME_TITLEBAR_HEIGHT,
		width - (FRAME_INSET*2 + FRAME_CONTENT_BORDER),
		height - (FRAME_INSET + FRAME_CONTENT_BORDER + FRAME_TITLEBAR_HEIGHT));
}

void Frame::setSize(int width, int height) {
	Component::setSize(width, height);
	
	content.setSize(width - (FRAME_INSET*2 + FRAME_CONTENT_BORDER*2),
		height - (FRAME_INSET + FRAME_CONTENT_BORDER*2 + FRAME_TITLEBAR_HEIGHT));
}

Dimension Frame::getPreferredSize(void) {
	return getMinimumSize();
}

Dimension Frame::getMinimumSize(void) {
	Dimension cd = content.getMinimumSize();
	
	return cd;
}

RefFrame Frame::hitTest(int x, int y) {
	for (RefFrame f = head; f; f = f->next) {
		if (f->rect.hit_test(x, y)) {
			return f;
		}
	}
	
	return RefFrame(0);
}

void Frame::repaintVisible(RefGraphics g, Rectangle *region) {
	for (RefFrame f = tail; f; f = f->prev) {
		if (f->isVisible()) {
			f->repaint(g);
			f->validate();
			
			if (region) {
				unite_rectangles(f->rect, *region);
			}
		}
	}
}

void Frame::prepend(RefFrame frame) {
	frame->next = head;
	frame->prev = RefFrame(0);
	
	if (head) {
		head->prev = frame;
	} else {
		tail = frame;
	}
	
	head = frame;
}

void Frame::append(RefFrame frame) {
	frame->next = RefFrame(0);
	
	if (head) {
		frame->prev = tail;
		tail->next = frame;
	} else {
		frame->prev = RefFrame(0);
		head = frame;
	}
	
	tail = frame;
}

void Frame::remove(RefFrame frame) {
	bool isHead = frame == head;
	bool isTail = frame == tail;
	
	if (isTail) {
		tail = frame->prev;
		
		if (!isHead) {
			frame->prev->next = RefFrame(0);
		}
	}
	
	if (isHead) {
		head = frame->next;
		
		if (!isTail) {
			frame->next->prev = RefFrame(0);
		}
	}
	
	if (!isHead && !isTail) {
		frame->prev->next = frame->next;
		frame->next->prev = frame->prev;
	}
	
	frame->next = frame->prev = RefFrame(0);
}

void Frame::toFront(void) {
	remove(self);
	prepend(self);
}

void Frame::toBack(void) {
	remove(self);
	append(self);
}

} // namespace gui
